import { getBaseUrl } from '@/services/urlconst';
import { getUserID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
const baseUrl = getBaseUrl();

/**
 * 获取标签
 * @param callback
 * @param controller
 * @returns
 */
const getTagService = (callback: (res: any) => void) => {
  const tagTask = useRequest(
    () => {
      const reqUrl = `${baseUrl}/tagController/getTagList`;
      let bodyParams = {
        createrID: getUserID(),
      };
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    tagTaskRun: tagTask?.run,
    tagTaskLoading: tagTask?.loading,
  };
};

/**
 * 添加标签
 * @param callback
 * @param controller
 * @returns
 */
const addTagService = (callback: (res: any) => void) => {
  const addTagTask = useRequest(
    (params) => {
      const reqUrl = `${baseUrl}/tagController/addTag`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
      };
      return requestFunComponFiex('POST', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    addTagTaskRun: addTagTask?.run,
    addTagTaskLoading: addTagTask?.loading,
  };
};

/**
 * 删除标签
 * @param callback
 * @param controller
 * @returns
 */
const delTagService = (callback: (res: any) => void) => {
  const delTagTask = useRequest(
    (params) => {
      const reqUrl = `${baseUrl}/tagController/delTag`;
      let bodyParams = {
        ...params,
        createrID: getUserID(),
      };
      return requestFunComponFiex('PUT', reqUrl, bodyParams);
    },
    {
      manual: true,
      onSuccess: callback,
    },
  );

  return {
    delTagTaskRun: delTagTask?.run,
    delTagTaskLoading: delTagTask?.loading,
  };
};

export { getTagService, addTagService, delTagService };
